﻿using Npoi.Mapper;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ZP.Common
{
    /// <summary>
    /// EXCEL
    /// </summary>
    public class ExcelHelper
    {
        /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="sm"></param>
        /// <param name="data"></param>
        /// <param name="SheetName"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool Export(Stream sm, List<object> data, string SheetName, string path)
        {
            bool blResult = false;
            /*try
            {
                var mapper = new Mapper(sm);
                mapper.Put(data, SheetName, false);
                mapper.Save(path);
                //mapper.Save(path, data, SheetName, false);
                blResult = true;
            }
            catch (Exception)
            {
                blResult = false;
            }*/
            return blResult;
        }



        public delegate bool CheckInfo(object obj);
        /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="strTempLateFile">模板文件</param>
        /// <param name="strGenarateDir">生成文件目录</param>
        /// <param name="strGenarateFileName">生成文件名</param>
        /// <param name="data">生成文件数据</param>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public static bool Export(string strTempLateFile, string strGenarateDir, string strGenarateFileName, List<object> data, string SheetName)
        {
            bool blResult = false;
            string strGenarateFile = "";
            try
            {
                if (!Directory.Exists(strGenarateDir))
                {
                    Directory.CreateDirectory(strGenarateDir);
                }
                strGenarateFile = System.IO.Path.Combine(strGenarateDir, strGenarateFileName);
                File.Copy(strTempLateFile, strGenarateFile, true);

                //method.1
                var mapper = new Mapper(strGenarateFile);
                mapper.Put(data, 0, false);
                mapper.Save(strGenarateFile);

                //method.2
                //var mapper = new Mapper();
                //mapper.Save(strGenarateFile, data, SheetName, overwrite: false);

                blResult = true;
            }
            catch (Exception ex)
            {
                blResult = false;
                if (File.Exists(strGenarateFile))
                {
                    File.Delete(strGenarateFile);
                }
                //Logger.Logger.Error(ex.Message);
            }
            return blResult;
        }

        /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="strTempLateFile">模板文件</param>
        /// <param name="strGenarateDir">生成文件目录</param>
        /// <param name="strGenarateFileName">生成文件名</param>
        /// <param name="data">生成文件数据</param>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public static bool Export2(string strTempLateFile, string strGenarateDir, string strGenarateFileName, Dictionary<string, List<object>> dataAndSheetName)
        {
            bool blResult = false;
            try
            {
                if (!Directory.Exists(strGenarateDir))
                {
                    Directory.CreateDirectory(strGenarateDir);
                }
                string strGenarateFile = System.IO.Path.Combine(strGenarateDir, strGenarateFileName);
                File.Copy(strTempLateFile, strGenarateFile, true);

                //method.1
                //var mapper = new Mapper(strGenarateFile);
                //mapper.Put(data, 0, false);
                //mapper.Save(strGenarateFile);

                //method.2
                var mapper = new Mapper();

                List<object> data = null;
                foreach (var SheetName in dataAndSheetName.Keys)
                {
                    data = dataAndSheetName[SheetName];
                    mapper.Save(strGenarateFile, data, SheetName);
                }
                blResult = true;
            }
            catch (Exception ex)
            {
                blResult = false;
                //Logger.Logger.Error(ex.Message);
            }
            return blResult;
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sm"></param>
        /// <param name="SheetName"></param>
        /// <returns></returns>
        public static List<T> Import<T>(Stream sm, string SheetName, List<string[]> arrmapperlst, CheckInfo chckinfo = null, string strErrorPath = null) where T : class
        {
            List<T> result = new List<T>();
            var importer = new Mapper(sm);
            string getSheet0Name = importer.Workbook.GetSheetName(0);
            SheetName = getSheet0Name;
            var sheets = importer.Workbook.GetSheet(getSheet0Name);
            var col = sheets.GetRow(0);
            col.CreateCell(col.Count()).SetCellValue("检测情况");

            foreach (string[] arrstr in arrmapperlst)
            {
                importer.Map<T>(arrstr[0], arrstr[1]);
            }

            var items = importer.Take<T>(SheetName, 0).ToList();
            if(items != null && items.Count > 0)
            {
                result = items.Select(p => p.Value).ToList();
            }
            /*items.ForEach(g =>
            {
                result.Add(g.Value);
            });*/

            if (chckinfo != null && !chckinfo(result))
            {
                importer.Put<T>(result, SheetName);
                importer.Save(strErrorPath);
                return null;
            }

            return result;
        }
    }
}
